package cn.edu.hit.education.view;

import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.part.ViewPart;
import org.springframework.stereotype.Component;

import cn.edu.hit.education.action.AddCollegeAction;
import cn.edu.hit.education.action.AddSpecialtyAction;
import cn.edu.hit.education.action.AddUniversityAction;
import cn.edu.hit.education.action.UpdateCollegeAction;
import cn.edu.hit.education.action.UpdateSpecialtyAction;
import cn.edu.hit.education.action.UpdateUniversityAction;
import cn.edu.hit.education.contentprovider.DepartmentContentProvider;
import cn.edu.hit.education.labelprovider.DepartmentLabelProvider;
import cn.edu.hit.education.model.CollegeNode;
import cn.edu.hit.education.model.DepartmentRootNode;
import cn.edu.hit.education.model.Node;
import cn.edu.hit.education.model.OrganizationNode;
import cn.edu.hit.education.model.SpecialtyNode;
import cn.edu.hit.education.model.UniversityNode;
import cn.edu.hit.education.pojo.College;
import cn.edu.hit.education.pojo.Specialty;
import cn.edu.hit.education.pojo.University;

@Component
public class DepartmentExploreView extends ViewPart {
	public static final String ID = "cn.edu.hit.education.view.DepartmentExploreView";
	private TreeViewer treeViewer;
	private Tree tree;
	private Node currentNode;

	public DepartmentExploreView() {
	}

	public void createPartControl(Composite parent) {

		treeViewer = new TreeViewer(parent, SWT.BORDER);
		tree = treeViewer.getTree();

		treeViewer.setContentProvider(new DepartmentContentProvider());
		treeViewer.setLabelProvider(new DepartmentLabelProvider());

		treeViewer.setInput(DepartmentRootNode.getRootNode());
		
		/////////////////////////////////////////
		makeContextMenu();
	}

	private void makeContextMenu() {
		MenuManager menuMgr = new MenuManager();

        Menu menu = menuMgr.createContextMenu(treeViewer.getControl());
        menuMgr.addMenuListener(new IMenuListener() {
            @Override
            public void menuAboutToShow(IMenuManager manager) {
                // IWorkbench wb = PlatformUI.getWorkbench();
                // IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
                if (treeViewer.getSelection().isEmpty()) {
                    return;
                }

                if (treeViewer.getSelection() instanceof IStructuredSelection) {
                    IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
                    currentNode = (Node)selection.getFirstElement();

                    if(currentNode instanceof OrganizationNode){
                    	menuMgr.add(new AddUniversityAction(DepartmentExploreView.this));
					}else if(currentNode instanceof UniversityNode){
						menuMgr.add(new AddCollegeAction(DepartmentExploreView.this,currentNode.getId()));
						menuMgr.add(new UpdateUniversityAction(DepartmentExploreView.this,currentNode.getId()));
					}else if(currentNode instanceof CollegeNode){
						menuMgr.add(new AddSpecialtyAction(DepartmentExploreView.this,currentNode.getId()));
						menuMgr.add(new UpdateCollegeAction(DepartmentExploreView.this,currentNode.getId()));
					}else{
						menuMgr.add(new UpdateSpecialtyAction(DepartmentExploreView.this,currentNode.getId()));
					} 
                }
            }
        });
        menuMgr.setRemoveAllWhenShown(true);
        treeViewer.getControl().setMenu(menu);
        
	}
	
	public void refreshNewData(Object obj){
		if(obj instanceof University){
			University university = (University)obj;
			UniversityNode universityNode = new UniversityNode(university.getId(),university.getName());
			currentNode.addChild(universityNode);
			//treeViewer.add(currentNode, universityNode);
		}else if(obj instanceof College){
			College college = (College)obj;
			CollegeNode collegeNode = new CollegeNode(college.getId(),college.getName());
			currentNode.addChild(collegeNode);
			//treeViewer.add(currentNode, collegeNode);
		}else if(obj instanceof Specialty){
			Specialty specialty = (Specialty)obj;
			SpecialtyNode specialtyNode = new SpecialtyNode(specialty.getId(),specialty.getName());
			currentNode.addChild(specialtyNode);
			//treeViewer.add(currentNode, specialtyNode);
		}else{
			
		}
		
		treeViewer.refresh(currentNode,true);
		treeViewer.setExpandedState(currentNode, true);
	}
	
	public void refreshUpdateData(Object obj){
		if(obj instanceof University){
			University university = (University)obj;
			currentNode.setName(university.getName());
		}else if(obj instanceof College){
			College college = (College)obj;
			currentNode.setName(college.getName());
		}else if(obj instanceof Specialty){
			Specialty specialty = (Specialty)obj;
			currentNode.setName(specialty.getName());
		}else{
			
		}
		
		treeViewer.refresh(currentNode,true);
		treeViewer.setExpandedState(currentNode, true);
	}
	
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		treeViewer.getControl().setFocus();
	}
}